﻿@{
    ViewData["Title"] = "角色维护";
}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" placeholder="名称" name="RoleName" id="RoleName">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-role-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>
            <div class="layui-row layui-col-space5">
                <div class="layui-col-xs9">
                    <table id="LAY-role-list" lay-filter="LAY-role-list"></table>
                </div>
                <div class="layui-col-xs3">
                    <div class="zTreeDemoBackground" style="text-align:center">
                        <ul id="rightTree" class="ztree"></ul>
                        <input type="hidden" id="treeRoleId" />
                        <button id="btnSearch" class="layui-btn" type="button" onclick="doGrant()"><i class="layui-icon"></i>授权</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="barBtn">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="pms">授权</a>
    <a class="layui-btn layui-btn-xs" lay-event="member">人员</a>
</script>
<link href="@Url.Content("~/lib/zTree/css/demo.css")" rel="stylesheet" />
<link href="@Url.Content("~/lib/zTree/css/bootstrapStyle/bootstrapStyle.css")" rel="stylesheet" />
<script src="@Url.Content("~/lib/zTree/js/jquery.ztree.all.min.js")"></script>
<script type="text/javascript">
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var table = layui.table
            , form = layui.form
            , $ = layui.$
            , admin = layui.admin;
        //数据绑定
        table.render({
            elem: '#LAY-role-list'
            , url: '/Role/GetList' //模拟接口
            , method: 'post'
            , height: 'full-180'
            , cols: [[
                { type: 'checkbox', fixed: 'left' }
                , { field: 'RoleCode', title: '角色代码', minWidth: 120 }
                , { field: 'RoleName', title: '角色名称', minWidth: 120 }
                , { field: 'Range', title: '范围', minWidth: 150 }
                , { field: 'RoleDesc', title: '备注', minWidth: 150 }
                , { field: 'IsDefault', title: '是否默认', width: 90 }
                , { field: 'right', title: '操作', minWidth: 220, align: 'center', fixed: 'right', toolbar: '#barBtn' }
            ]]
            , page: true
            , limit: 10
            , limits: [10, 15, 20, 25, 30]
            , text: { none: '暂无数据' }
        });

        //监听工具条
        table.on('tool(LAY-role-list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除记录？', function (index) {
                    admin.req({
                        url: "/Role/DelByIds1?Id=" + data.Id //实际使用请改成服务端真实接口
                        , method: 'post'
                        , done: function (res) {
                            //登入成功的提示与跳转
                            layer.msg(res.msg, {
                                offset: '50px'
                                , icon: 1
                                , time: 3000
                            }, function () {
                                table.reload('LAY-role-list'); //重载表格
                            });
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '编辑角色'
                    , content: '/Role/RoleAdd?Id=' + data.Id
                    , area: ["600px", "400px"]
                    , maxmin: true
                });
            } else if (obj.event == "pms") {
                GetTree(data.Id);
            } else if (obj.event == "member") {
                layer.open({
                    type: 2
                    , title: '人员信息'
                    , content: '/Role/RelMemberList?RoleId=' + data.Id
                    , area: ["800px", "500px"]
                    , maxmin: true
                });
            }
        });
        //监听搜索
        form.on('submit(LAY-role-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-role-list', {
                where: field
            });
        });

        var $ = layui.$, active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-role-list')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                layer.confirm('确定删除吗？', function (index) {
                    var Ids = "";
                    $.each(checkData, function (index, val) {
                        Ids += val.Id + ",";
                    });
                    admin.req({
                        url: '/Role/DelByIds1' //实际使用请改成服务端真实接口
                        , method: 'post'
                        , data: "Ids=" + Ids
                        , done: function (res) {
                            //登入成功的提示与跳转
                            layer.msg(res.msg, {
                                offset: '50px'
                                , icon: 1
                                , time: 3000
                            }, function () {
                                table.reload('LAY-role-list'); //重载表格
                            });
                        }
                    });
                });
            },
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加角色'
                    , content: '/Role/RoleAdd?Id=0'
                    , area: ["600px", "400px"]
                    , maxmin: true
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        GetTree("");
    });

    function GetTree(roleId) {
        $.post("/Role/GetPmsByRole", "roleId=" + roleId, function (data) {
            initzTree(data.rows, roleId);
            var treeObj = $.fn.zTree.getZTreeObj("rightTree");
            treeObj.expandAll(true);
        });

    }

    function initzTree(data, roleId) {
        $("#treeRoleId").val(roleId);
        var setting = {
            data: {
                key: {
                    name: "PmsName",
                    title: "PmsName",
                    checked: "Checked"
                },
                simpleData: {
                    enable: true,
                    idKey: "Id",
                    pIdKey: "ParentId",
                    rootPId: 0
                }
            },
            check: {
                enable: true
            }
        };
        $.fn.zTree.init($("#rightTree"), setting, data);

    }

    function doGrant() {
        var treeObj = $.fn.zTree.getZTreeObj("rightTree");
        var nodes = treeObj.getCheckedNodes(true);
        var rights = "";
        for (var i = 0; i < nodes.length; i++) {
            if (i != nodes.length - 1)
                rights += nodes[i].Id + "|";
            else
                rights += nodes[i].Id
        }

        layui.admin.req({
            url: '/Role/Grant' //实际使用请改成服务端真实接口
            , method: 'post'
            , data: "RoleId=" + $("#treeRoleId").val() + "&Rights=" + rights
            , done: function (res) {
                //登入成功的提示与跳转
                layer.msg(res.msg, {
                    offset: '50px'
                    , icon: 1
                    , time: 3000
                });
            }
        });
    }
</script>
